package demo.JUnit5;

import java.util.Iterator;
import java.util.Optional;
import java.util.Set;

import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.discovery.ClassNameFilter;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

import com.titan.core.utils.data.StringUtils;

public class PlatformDemo01_查找测试类 {
    public static void main(String[] args) {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectPackage("demo.JUnit5")
                         , DiscoverySelectors.selectClass(PlatformDemo01_查找测试类.class))
                .filters(ClassNameFilter.includeClassNamePatterns(//".*",
                          "demo.JUnit5.JupiterDemo04_Assumptions假设"
                        , "demo.JUnit5.JupiterDemo05_测试执行顺序"
                        ))
                .build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        System.out.println(StringUtils.rightPad("DisplayName", 50)
                + StringUtils.rightPad("Source", 100)
                + "UniqueId"
                );
        print(testPlan, testPlan.getRoots());
    }
    private static void print(TestPlan testPlan, Set<TestIdentifier> items) {
        for (Iterator iterator = items.iterator(); iterator.hasNext();) {
            TestIdentifier identifier = (TestIdentifier) iterator.next();
            Optional<TestSource> optional = identifier.getSource();
            String uniqueId = identifier.getUniqueId();
            if(optional.isPresent()) {
                if (optional.get() instanceof ClassSource) {
                    ClassSource source = (ClassSource) optional.get();
                    System.out.println(StringUtils.rightPad("class:"+identifier.getDisplayName(), 50)
                            + StringUtils.rightPad(source.getClassName(), 100)
                            + uniqueId
                            );
                } else if(optional.get() instanceof MethodSource) {
                    MethodSource source = (MethodSource) optional.get();
                    System.out.println(StringUtils.rightPad("method:"+identifier.getDisplayName(), 50)
                            + StringUtils.rightPad(source.getClassName()+"#"+source.getMethodName(), 100)
                            + uniqueId
                            );
                } else {
                    System.out.println(StringUtils.rightPad(identifier.getDisplayName(), 50)
                            + StringUtils.rightPad(optional.get().getClass().getName(), 100)
                            + uniqueId
                            );
                }
            } else {
                System.out.println(StringUtils.rightPad("engine:"+identifier.getDisplayName(), 50)
                        + StringUtils.repeat(' ', 100)
                        + uniqueId
                        );
            }
            Set<TestIdentifier> childrem = testPlan.getChildren(uniqueId);
            if(childrem.size()>0) {
                print(testPlan, childrem);
            }
        }        
    }
    
    
}
